package com.sec.android.app.myfiles.module.shortcut;

import android.content.Context;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.facade.cmd.DeleteRecordCmd;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.local.home.HomeMenuImp;

import java.util.ArrayList;

public class ShortcutMenuImp extends HomeMenuImp {
    public ShortcutMenuImp(Context context, FileRecord.StorageType supportStorage) {
        super(context, supportStorage);
    }

    @Override
    public void onPrepareEditOptionsMenu(Menu menu) {
        setMenuItemVisibility(menu, R.id.menu_remove_shortcut, mFragment.getSelectedFileCount() > 0);
    }

    @Override
    public boolean _onOptionsItemSelected(MenuItem item) {
        boolean bRet = true;
        int id = item.getItemId();
        switch (id) {
            case R.id.menu_remove_shortcut:
                SamsungAnalyticsLog.sendLog(mFragment.getProcessId(), SamsungAnalyticsLog.Event.REMOVE, SamsungAnalyticsLog.SelectMode.SELECTION_MODE);
                MyFilesFacade.deleteRecord(mFragment.getProcessId(), mContext, mFragment, mFragment.getSelectedFile(),
                        DeleteRecordCmd.OperationProgress.DO_OPERATE);
                break;
            default:
                bRet = false;
                break;
        }
        return bRet;
    }

    @Override
    protected void createContextualMenuOnBackground(ContextMenu menu) {
        if (mFragment.getSelectedFileCount() > 0) {
            menu.add(0, R.id.menu_remove_shortcut, 0, R.string.menu_remove);
        }
    }

    @Override
    protected void createContextualMenuOnLeftPanel(ContextMenu menu, FileRecord record) {
        menu.add(0, R.id.menu_remove, 0, R.string.menu_remove);
        menu.add(0, R.id.menu_manage_shortcut, 0, R.string.manage_shortcut);
    }

    @Override
    protected void _createContextualMenuOnItems(ContextMenu menu, FileRecord record, boolean isForOneItem) {
        menu.add(0, R.id.menu_remove_shortcut, 0, R.string.menu_remove);
    }

    @Override
    public boolean _onContextualItemSelected(MenuItem item, ArrayList<FileRecord> srcRecordList, FileRecord record, AbsMyFilesFragment fragment) {
        srcRecordList = new ArrayList<>();
        srcRecordList.add(record);
        return super._onContextualItemSelected(item, srcRecordList, record, fragment);
    }
}
